package wmh.day0109.dao.impl;

import  wmh.day0109.dao.UserDao;
import  wmh.day0109.entity.User;
import  wmh.day0109.utils.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;


public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public List<User> queryAllUser(Map<String, Object> map) {
        List<User> users = new ArrayList<>();
        //2.创建lottery对象，用来存储单条lottery数据
        User user = null;
        //3.定义查询User表所有数据的sql语句
        String sql = "select id,user_name,mark_no,status,phone,open_date from lottery";
        //4.调用公共的查询rs结果集方法
        super.rs = super.executeQuery(sql);
        //5.循环遍历rs结果集
        try {
            while (rs.next()) {
                //实例化User对象
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("user_name"));
                user.setMarkNo(rs.getString("mark_no"));
                user.setStatus(rs.getInt("status"));
                user.setPhone(rs.getString("phone"));
                user.setDate(rs.getString("open_date"));
                //将赋值后的单条对象数据存储到depts集合对象中
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAllConn(conn, pstmt, rs);
        }
        return users;
    }

    @Override
    public int saveUser(User user) {
        int addRes = 0;
        String sql = "insert into lottery(user_name,mark_no,status,phone,open_date) values(?,?,?,?,to_date(?,'yyyy-mm-dd'))";
        addRes = super.executeUpdate(sql, new Object[]{user.getUsername(), user.getMarkNo(), user.getStatus(), user.getPhone(), user.getDate()});
        return addRes;
    }


//随即编码
    public static String getNumSmallCharRandom(int length) {
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出小写字母
                val += (char)(random.nextInt(26) + 97);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                //输出数字
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
}



